package org.ouwh.oowiki.http;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.ouwh.fw.model.data.Struct;
import org.ouwh.fw.model.service.Service;

public class VelocityService extends Service {
	private static Log log = LogFactory.getLog(VelocityService.class);
	
	private final static String RESOURCE_LOADER = "resource-loader";
	private final static String FILE_RESOURCE_LOADER_PATH = "resource-path";
	
	private String resourceLoader;
	private String reourcePath;
	
	private VelocityEngine ve;
	
	@Override
	protected void dumpToStruct(Struct struct) {
		struct.addData(RESOURCE_LOADER, resourceLoader);
		struct.addData(FILE_RESOURCE_LOADER_PATH, reourcePath);
	}

	@Override
	protected void init(Properties properties) {
		resourceLoader = properties.getProperty(RESOURCE_LOADER);
		reourcePath = properties.getProperty(FILE_RESOURCE_LOADER_PATH);
	}

	@Override
	protected void reset(Properties properties) throws Exception {
		shutdown();
		startup();
	}

	@Override
	protected void shutdown() throws Exception {
		log.info("Velocity Service stopped.");
	}

	@Override
	protected void startup() throws Exception {
	    ve = new VelocityEngine();
	    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, resourceLoader);
	    ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, reourcePath);
	    ve.init();
		log.info("Velocity Service started.");
	}

	public VelocityEngine getEngine() {
		return ve;
	}

}
